package asia.njut.reader.activity;

import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import asia.njut.reader.R;
import asia.njut.reader.R.layout;
import asia.njut.reader.R.menu;
import asia.njut.reader.utils.MD5Util;


public class NfcActivity extends Activity {   
  NfcAdapter nfcAdapter;   
  TextView promt;   
  String TAG="NfcActivity";
  String Blosks="";
  Intent intent;
  @Override   
  public void onCreate(Bundle savedInstanceState) {   
      super.onCreate(savedInstanceState);   
      Log.v(TAG,TAG+" started");
      setContentView(R.layout.activity_nfc);   
      promt = (TextView) findViewById(R.id.promt);   
      // 获取默认的NFC控制器   
      nfcAdapter = NfcAdapter.getDefaultAdapter(this);   
      if (nfcAdapter == null) {   
          promt.setText("设备不支持NFC！");  
          Log.v(TAG,"设备不支持NFC！");
         // finish();   
          return;   
      }   
      if (!nfcAdapter.isEnabled()) {   
          promt.setText("请在系统设置中先启用NFC功能！");   
          Log.v(TAG,"请在系统设置中先启用NFC功能！");
         // finish();   
          return;   
      }if (nfcAdapter != null) {   
          Log.v(TAG,"设备启动NFC！");
         
      }      
      
  }   
 
  @Override   
  protected void onResume() {   
      super.onResume();   
      //得到是否检测到ACTION_TECH_DISCOVERED触发   
      
   
      if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(getIntent().getAction())) {   
          //处理该intent   
      	Log.v(TAG,"getAction");
          processIntent(getIntent());   
      }   
  }   
  //字符序列转换为16进制字符串   
  private String bytesToHexString(byte[] src) {   
      StringBuilder stringBuilder = new StringBuilder("0x");   
      if (src == null || src.length <= 0) {   
          return null;   
      }   
      char[] buffer = new char[2];   
      for (int i = 0; i < src.length; i++) {   
          buffer[0] = Character.forDigit((src[i] >>> 4) & 0x0F, 16);   
          buffer[1] = Character.forDigit(src[i] & 0x0F, 16);   
          stringBuilder.append(buffer);   
      }   
      return stringBuilder.toString();   
  }   
 
  /**  
   * Parses the NDEF Message from the intent and prints to the TextView  
   */   
  private void processIntent(Intent intent) {   
      //取出封装在intent中的TAG   
      Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);   

      boolean auth = false;   
      //读取TAG   
      MifareClassic mfc = MifareClassic.get(tagFromIntent);   
      try {   
          String metaInfo = "";   
          mfc.connect();   
          int type = mfc.getType();//获取TAG的类型   
          int sectorCount = mfc.getSectorCount();//获取TAG中包含的扇区数   
          String typeS = "";   
          switch (type) {   
          case MifareClassic.TYPE_CLASSIC:   
              typeS = "TYPE_CLASSIC";   
              break;   
          case MifareClassic.TYPE_PLUS:   
              typeS = "TYPE_PLUS";   
              break;   
          case MifareClassic.TYPE_PRO:   
              typeS = "TYPE_PRO";   
              break;   
          case MifareClassic.TYPE_UNKNOWN:   
              typeS = "TYPE_UNKNOWN";   
              break;   
          }   
        for (int j = 0; j < sectorCount; j++) {   
              //Authenticate a sector with key A.   
              auth = mfc.authenticateSectorWithKeyA(j,   
                      MifareClassic.KEY_DEFAULT);   
              int bCount;   
              int bIndex;   
              if (auth) {   
             
                  // 读取扇区中的块   
                  bCount = mfc.getBlockCountInSector(j);   
                  bIndex = mfc.sectorToBlock(j);   
                  for (int i = 0; i < bCount; i++) {   
                      byte[] data = mfc.readBlock(bIndex);   
                      bIndex++;  
                      Blosks+=bytesToHexString(data);
                  }   
              } else {   
              	 promt.setText("验证失败"); 
              }   
          }   
          promt.setText("卡片ID "+MD5Util.MD5(Blosks)); 
          
      
          //intent.putExtra("TAGID", MD5Util.MD5(Blosks));        
          String tag=MD5Util.MD5(Blosks);
          Log.i("nfc", tag);
  		
			
      } catch (Exception e) {   
          e.printStackTrace();   
      }   
  }   
}  